package screen.component
{
	import com.adobe.serialization.json.JSON;
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.utils.setTimeout;
	
	import flashx.textLayout.utils.CharacterUtil;
	
	import helper.Services;
	import helper.ValueObject;
	
	import mx.utils.NameUtil;
	
	import org.osmf.events.TimeEvent;
	
	import screen.ManulifeScreen;

	public class Popup extends UI
	{
		private var _popup_mc:MovieClip
		private const _TUTO_NO1:Number = 11;
		private const _TUTO_NO2:Number = 4;
		private const _GIFT_NAME:String = "G"
		private const _TUTORIAL_AVATAR_FOLDER:String="../assets/tutorial";
		private const _TUTORIAL_UNIT_WIDTH:Number = 480;
		private const _current_cami_airplane_arr:Array=[];
		private var _current_group:MovieClip;
		private var _tutorial_group1:MovieClip;
		private var _tutorial_group2:MovieClip;
		private var _bg:MovieClip;
		private var _remain_time:Number =120;
		public static var instance:Popup;
		public function Popup()
		{
			if(!instance){
				instance = this;
			}
			init()
		}
		public static function getInsctance():Popup{
			if(!instance){
				instance = new Popup()
			}
			return instance;
		}
		private function init():void{
			_popup_mc = ValueObject.C1_RESOURCE.getExportedAsset("Popup") as MovieClip;
			popupInit(_popup_mc);
			_tutorial_group1 = new MovieClip();
			_popup_mc.addChild(_tutorial_group1);
			_tutorial_group2 = new MovieClip();
			_tutorial_group2.visible = false;
			_popup_mc.addChild(_tutorial_group2);
			
			_bg = ManulifeScreen.getInstance().background;
		
			with(_popup_mc){
				_tutorial_group1.x=_tutorial_group2.x = maskMc.x;
				_tutorial_group1.y =_tutorial_group2.y = maskMc.y;
				_current_group = _tutorial_group1;
				_current_group.mask = maskMc;
				sliderInit(_tutorial_group1,1,1,_tutorial_group1.x,_TUTORIAL_UNIT_WIDTH);
				sliderInit(_tutorial_group2,1,1,_tutorial_group2.x,_TUTORIAL_UNIT_WIDTH);
				
				buttonImageInit(nextBtn);
				MovieClip(nextBtn).addEventListener(MouseEvent.CLICK,nextTutoHandle);
				buttonImageInit(preBtn);
				MovieClip(preBtn).addEventListener(MouseEvent.CLICK,preTutoHandle);
				buttonImageInit(closeBtn);
				MovieClip(closeBtn).addEventListener(MouseEvent.CLICK,hide);
			}
			
			for(var i:Number=1;i<=_TUTO_NO1;i++){
				var tutorial_unit1:MovieClip = ValueObject.C1_RESOURCE.getExportedAsset("tutorialUnit") as MovieClip;
				tutorial_unit1.avatar.load(new URLRequest(_TUTORIAL_AVATAR_FOLDER + "1/" + i + ".png"));
				tutorial_unit1.x = (i-1)*_TUTORIAL_UNIT_WIDTH;
				_tutorial_group1.addChild(tutorial_unit1);
			}
			
			updateSliderBtn(_current_group);
			with(_bg){
				buttonImageInit(ruleBtn);
				MovieClip(ruleBtn).addEventListener(MouseEvent.CLICK,tabBtnHandle);
				buttonImageInit(giftBtn);
				MovieClip(giftBtn).addEventListener(MouseEvent.CLICK,getStore);
				buttonImageInit(inviteFriendBtn);
			}
		}
		private function getStore(e:MouseEvent):void{
			Services.getInstance().getStore(getStoreComplete,getStoreError);
			tabBtnHandle(e);
		}
		private function getStoreComplete(e:Event):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getStoreComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getStoreError);
			var data:Object = JSON.decode(e.target.data);
			for(var i:Number=3;i<9;i++){
				var old_gift:MovieClip =  getGift(i.toString())
				if(old_gift){
					TextField(old_gift.quanlityTf).text ="Bạn đã nhận được:" +data[i.toString()];
				}else{
					var gift:MovieClip = ValueObject.C1_RESOURCE.getExportedAsset("giftUnit") as MovieClip;
					if(i==3){
						gift.buttonMode = true;
						gift.addEventListener(MouseEvent.CLICK,navDiemTuaTinhThan);
					}
					gift.avatar.load(new URLRequest(_TUTORIAL_AVATAR_FOLDER + "2/" + i + ".png"));
					gift.x = _tutorial_group2.numChildren*_TUTORIAL_UNIT_WIDTH;
					TextField(gift.quanlityTf).text ="Bạn đã nhận được:" +data[i.toString()];
					_tutorial_group2.addChild(gift);
				}
			}
			updateSliderBtn(_current_group);
		}
		private function getGift(name:String):MovieClip{
			var mc:MovieClip = _tutorial_group2.getChildByName(_GIFT_NAME + name) as MovieClip;
			if(mc){
				return mc;
			}
			return null;
		}
		private function navDiemTuaTinhThan(e:MouseEvent):void{
			navigateToURL(new URLRequest("http://diemtuatinhthan.com/card"));
		}
		private function getStoreError(e:IOErrorEvent):void{
			URLLoader(e.target).removeEventListener(Event.COMPLETE,getStoreComplete);
			URLLoader(e.target).removeEventListener(IOErrorEvent.IO_ERROR,getStoreError);
		}
		public function tabBtnHandle(e:MouseEvent):void{
			var btn:MovieClip = e.currentTarget as MovieClip;
			OtherAirplane.getInstance().hideBtnPlayAndRule();
			switch(btn.name){
				case "ruleBtn":
				case "ruleBtn2":
					if(_current_group != _tutorial_group1&&_popup_mc.scaleX == 1){
						hide();
						setTimeout(show,500);
					}else if(_current_group != _tutorial_group1 || _popup_mc.scaleX <= 1){
						show();
					}
					_current_group = _tutorial_group1;
					_tutorial_group1.visible = true;
					_tutorial_group2.visible = false;
					break;
				case "giftBtn":
					if(_current_group != _tutorial_group2&&_popup_mc.scaleX == 1){
						hide();
						setTimeout(show,500,2);
					}else if(_current_group != _tutorial_group2 || _popup_mc.scaleX < 1){
						show();
					}
					_current_group = _tutorial_group2;
					_tutorial_group1.visible = false;
					_tutorial_group2.visible = true;
					break;
			}
			_current_group.mask =_popup_mc.maskMc;
			updateSliderBtn(_current_group);
		}
		private function nextTutoHandle(e:MouseEvent):void{
			sliderNextClick(_current_group);
		}
		private function preTutoHandle(e:MouseEvent):void{
			sliderPreClick(_current_group);
		}
		public function show(no:Number=1):void{
			MovieClip(_bg.lableMc).gotoAndStop(no);
			TweenLite.to( _bg.lableMc,0.5, { y:23,alpha:1,ease:Back.easeOut} );
			popupShow(_popup_mc);
			ManulifeScreen.getInstance().removeEventListener(MouseEvent.CLICK,Tank.getInstance().shootAirPlane);
			ManulifeScreen.getInstance().removeEventListener(MouseEvent.MOUSE_MOVE,Tank.getInstance().mouseMoveHandle);
		}
		public function get onShow():Boolean{
			return _popup_mc.visible;
		}
		public function hide(e:MouseEvent=null):void{
			if(!_popup_mc.visible){
				return;
			}
			TweenLite.to( _bg.lableMc,0.5, { y:-60,alpha:0} );
			popupHide(_popup_mc);
			setTimeout(addEventClick,1000);
		}
		private function addEventClick():void{
			ManulifeScreen.getInstance().addEventListener(MouseEvent.CLICK,Tank.getInstance().shootAirPlane);
			ManulifeScreen.getInstance().addEventListener(MouseEvent.MOUSE_MOVE,Tank.getInstance().mouseMoveHandle);
		}
		
	}
}